﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ajax_handler : System.Web.UI.Page
{
    [System.Web.Services.WebMethod]
    public static string HelloWorld()
    {
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
        System.Collections.Hashtable ht = new System.Collections.Hashtable();
        ht.Add("d", "Hello World");
        return js.Serialize(ht);
    }
    [System.Web.Services.WebMethod()]
    public static string execute(string sp_name, Hashtable sp_params, Hashtable db_options)
    {
        string connectionString = null;
        if (db_options.ContainsKey("connection_string_name"))
        {
            //if (db_options["connection_string_name"] == "master")
            //{
            //    connectionString = BizGlobalModule.HoConnectionStringName;
            //}
            //else if (db_options["connection_string_name"] == "branch")
            //{
            //    connectionString = Tssipl.Bank.SessionHelper.BranchConnectionStringName;
            //}
            //else
            {
                connectionString = db_options["connection_string_name"].ToString();
            }
        }
        else
        {
            connectionString = "DefaultConnectionStringName";
        }

        string procedureName = sp_name;
        Dictionary<string, object> parameters = null;
        AdDataAccessOptions options = null;
        DataSet ds = null;
        string summary = string.Empty;
        try
        {
            parameters = new Dictionary<string, object>();
            foreach (string key in sp_params.Keys)
            {
                parameters.Add(key, sp_params[key]);
            }
            options = new AdDataAccessOptions();
            var _with1 = options;
            _with1.StoredProcedureName = procedureName;
            _with1.StoredProcedureParamList = parameters;
            _with1.ConnectionInfo = new AdConnectionInfo(connectionString);
            if (db_options.ContainsKey("timeout"))
            {
                options.CommandTimeout =int.Parse( db_options["timeout"].ToString());
            }
            ds = AdDataAccessComponent.Execute(ref options);

            DataTable miscDataTable = new DataTable("misc");
            miscDataTable.Columns.Add("name", typeof(string));
            miscDataTable.Columns.Add("value", typeof(string));

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            miscDataTable.Rows.Add("params", serializer.Serialize(parameters));
            ds.Tables.Add(miscDataTable);

            return Ad.JsonEx.JsonUtil.ToJSONString(ds);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}